//扩展 jQuery 中的cookie操作
layui.define("jquery", function(exports) {
    var $ = layui.jquery;
    (function(factory) {
        if (typeof define === 'function' && define.amd) {
            // AMD
            define(['jquery', 'datatables.net'], function($) {
                return factory($, window, document);
            });
        } else if (typeof exports === 'object') {
            // CommonJS
            module.exports = function(root, $) {
                if (!root) {
                    root = window;
                }

                //if (!$ || !$.fn.dataTable) {
                //$ = require('datatables.net')(root, $).$;
                //}

                return factory($, root, root.document);
            };
        } else {
            // Browser
            factory(jQuery, window, document);
        }
    }(function($, window, document) {
        'use strict';

        if (!$.setCookie) {
            $.extend({
                setCookie: function(c_name, value, exdays) {
                    try {
                        if (!c_name) return false;
                        var exdate = new Date();
                        exdate.setDate(exdate.getDate() + exdays);
                        var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
                        document.cookie = c_name + "=" + c_value;
                    } catch (err) {
                        return '';
                    };
                    return '';
                }
            });
        }

        if (!$.getCookie) {
            $.extend({
                getCookie: function(c_name) {
                    try {
                        var i, x, y,
                            ARRcookies = document.cookie.split(";");
                        for (i = 0; i < ARRcookies.length; i++) {
                            x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                            y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                            x = x.replace(/^\s+|\s+$/g, "");
                            if (x == c_name) return (unescape(y));
                        };
                    } catch (err) {
                        return '';
                    };
                    return '';
                }
            });
        }

        if (!$.delCookie) {
            $.extend({
                delCookie: function(c_name) {
                    try {
                        var exp = new Date();
                        exp.setTime(exp.getTime() - 10000);
                        document.cookie = c_name + "=1; expires=" + exp.toGMTString() + ""; //前台要删除";path=/"才能删除cookie，后台不用
                    } catch (err) {
                        return '';
                    };
                    return '';
                }
            });
        }


    }));

    exports("cookieMod");
});